package com.xyz.rok.tools.controller;

import com.xyz.rok.tools.constant.SystemCode;
import com.xyz.rok.tools.constant.Task;
import com.xyz.rok.tools.model.Command;
import com.xyz.rok.tools.model.Result;
import com.xyz.rok.tools.service.CommandService;
import com.xyz.rok.tools.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/4/14 17:29
 **/
@Slf4j
@RestController
@RequestMapping("/api/common")
public class CommonController {

    private final int[] whiteRgbRange = {110, 255, 150, 255, 150, 255};
    private final int[] blackRgbRange = {0, 140, 0, 140, 0, 140};
    private final int[] blueRgbRange = {60, 99, 160, 193, 200, 220};
    @Value("${upload.path:/app/upload/}")
    private String uploadPath;
    @Value("${traineddata.path:/app/traineddata/}")
    private String dataPath;
    @Autowired
    private ConfigService configModelService;
    @Autowired
    private CommandService commandService;

    private ConcurrentLinkedQueue<Command> commandQueue = new ConcurrentLinkedQueue();

//    @PostMapping("/upload")
//    @ResponseBody
//    public Result upload(@RequestBody UploadModel img) {
//        String imgFilePath = uploadPath + UUID.randomUUID().toString() + ".png";
//        OCRUtil.generateImage(img.getImg(), imgFilePath);
//        return Result.success();
//    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        File dest = new File(uploadPath + fileName);
        try {
            file.transferTo(dest);
            return Result.success();
        } catch (IOException e) {
            log.error("文件上传失败", e);
        }
        return Result.error(SystemCode.SYS_UPLOAD_FAILED);
    }

    @GetMapping("/config")
    public Result config() {
        return Result.success(configModelService.selectConfig());
    }


    @GetMapping("/configByKey")
    public Result configByKey(@RequestParam("key") String key) {
        return Result.success(configModelService.selectByKey(key));
    }

    @GetMapping("/configByLikeKey")
    public Result selectByLikeKey(@RequestParam("key") String key) {
        return Result.success(configModelService.selectByLikeKey(key));
    }

    @GetMapping("/serverConfig")
    public Result serverConfig(@RequestParam("serverId") Integer serverId, @RequestParam("key") String key) {
        return Result.success(configModelService.selectServerConfig(serverId, key));
    }

    @GetMapping("/getServerId")
    public Result serverConfig(@RequestParam("sn") String sn) {
        return Result.success(configModelService.selectByKey(sn));
    }


    @GetMapping("/command")
    public Result command() {
        return Result.success(commandService.selectUnExecutedCommand());
    }

    @PostMapping("/beginExecute")
    public Result executeBegin(@RequestBody Command command) {
        return Result.success(commandService.updateBeginTime(command.getId()));
    }

    @PostMapping("/endExecute")
    public Result executedOver(@RequestBody Command command) {
        return Result.success(commandService.updateEndTime(command.getId()));
    }

    @GetMapping("/getTaskStatus")
    public Result getTaskStatus(@RequestParam("unionId") Long unionId) {
        return Result.success(commandService.getLastRunningStatus(unionId, Task.EXTRACT_UNION_TASK));
    }
}
